package com.njupt.protal.controller;

import com.njupt.common.pojo.JsonUtils;
import com.njupt.content.service.ItemContentService;
import com.njupt.pojo.TbContent;
import com.njupt.protal.pojo.AD1DTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 14:39 2018/5/28
 * @Modified By:
 **/
@Controller
public class IndexController {

    @Autowired
    private ItemContentService itemContentService;

    @Value("${AD1_CATEGORY_ID}")
    private Long AD1_CATEGORY_ID;

    @Value("${AD1_CATEGORY_HEIGHT}")
    private Integer AD1_CATEGORY_HEIGHT;

    @Value("${AD1_CATEGORY_WIDTH}")
    private Integer AD1_CATEGORY_WIDTH;

    @Value("${AD1_CATEGORY_HEIGHT_B}")
    private Integer AD1_CATEGORY_HEIGHT_B;

    @Value("${AD1_CATEGORY_WIDTH_B}")
    private Integer AD1_CATEGORY_WIDTH_B;


    @RequestMapping("/index")
    public String showIndex(Model model){

       List<TbContent> contentList = itemContentService.getContentByCid(AD1_CATEGORY_ID);
       List<AD1DTO> list = new ArrayList<>();
        for (TbContent content: contentList
             ) {
            AD1DTO dto = new AD1DTO();
            dto.setAlt(content.getTitle());
            dto.setSrc(content.getPic());
            dto.setSrcB(content.getPic2());
            dto.setHref(content.getUrl());
            dto.setHeight(AD1_CATEGORY_HEIGHT);
            dto.setHeightB(AD1_CATEGORY_HEIGHT_B);
            dto.setWidth(AD1_CATEGORY_WIDTH);
            dto.setWidthB(AD1_CATEGORY_WIDTH_B);
            list.add(dto);

        }
        System.out.println("----------"+list);
        model.addAttribute("ad1", JsonUtils.objectToJson(list));
        return "index";
    }
}
